home *** CD-ROM | disk | FTP | other *** search
/ This Disc Bytes! / Power Computing - The Disc 2 - This Disc Bytes.ISO / mac / CodeWarrior 7 Lite for 68K / MacOS Support / Headers / Universal Headers / DigitalSignature.h < prev    next >
Text File  |  1995-07-06  |  17KB  |  398 lines

  1. /*
  2.      File:        DigitalSignature.h
  3.  
  4.      Contains:    Digital Signature Interfaces.
  5.  
  6.      Version:    Technology:    AOCE toolbox 1.02
  7.                  Package:    Universal Interfaces 2.1 in “MPW Latest” on ETO #18
  8.  
  9.      Copyright:    © 1984-1995 by Apple Computer, Inc.
  10.                  All rights reserved.
  11.  
  12.      Bugs?:        If you find a problem with this file, use the Apple Bug Reporter
  13.                  stack.  Include the file and version information (from above)
  14.                  in the problem description and send to:
  15.                      Internet:    apple.bugs@applelink.apple.com
  16.                      AppleLink:    APPLE.BUGS
  17.  
  18. */
  19.  
  20. #ifndef __DIGITALSIGNATURE__
  21. #define __DIGITALSIGNATURE__
  22.  
  23.  
  24. #ifndef __TYPES__
  25. #include <Types.h>
  26. #endif
  27. /*    #include <ConditionalMacros.h>                                */
  28.  
  29. #ifndef __MEMORY__
  30. #include <Memory.h>
  31. #endif
  32. /*    #include <MixedMode.h>                                        */
  33.  
  34. #ifndef __FILES__
  35. #include <Files.h>
  36. #endif
  37. /*    #include <OSUtils.h>                                        */
  38. /*    #include <Finder.h>                                            */
  39.  
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43.  
  44. #if PRAGMA_ALIGN_SUPPORTED
  45. #pragma options align=mac68k
  46. #endif
  47.  
  48. #if PRAGMA_IMPORT_SUPPORTED
  49. #pragma import on
  50. #endif
  51.  
  52.  
  53. enum {
  54.     kSIGNewContext                = 1900,
  55.     kSIGDisposeContext            = 1901,
  56.     kSIGSignPrepare                = 1902,
  57.     kSIGSign                    = 1903,
  58.     kSIGVerifyPrepare            = 1904,
  59.     kSIGVerify                    = 1905,
  60.     kSIGDigestPrepare            = 1906,
  61.     kSIGDigest                    = 1907,
  62.     kSIGProcessData                = 1908,
  63.     kSIGShowSigner                = 1909,
  64.     kSIGGetSignerInfo            = 1910,
  65.     kSIGGetCertInfo                = 1911,
  66.     kSIGGetCertNameAttributes    = 1912,
  67.     kSIGGetCertIssuerNameAttributes = 1913,
  68.     kSIGFileIsSigned            = 2500,
  69.     kSIGSignFile                = 2501,
  70.     kSIGVerifyFile                = 2502
  71. };
  72.  
  73. enum {
  74.     kSIGCountryCode,
  75.     kSIGOrganization,
  76.     kSIGStreetAddress,
  77.     kSIGState,
  78.     kSIGLocality,
  79.     kSIGCommonName,
  80.     kSIGTitle,
  81.     kSIGOrganizationUnit,
  82.     kSIGPostalCode
  83. };
  84.  
  85. typedef unsigned short SIGNameAttributeType;
  86.  
  87. /* 
  88. Certificate status codes returned in SIGCertInfo or SIGSignerInfo from
  89. either SIGGetCertInfo or SIGGetSignerInfo respectively. kSIGValid means that
  90. the certificate is currently valid. kSIGPending means the certificate is
  91. currently not valid - but will be.  kSIGExpired means the certificate has
  92. expired. A time is always associated with a SIGCertStatus.  In each case the
  93. time has a specific interpretation.  When the status is kSIGValid the time is
  94. when the certificate will expire. When the status is kSIGPending the time is
  95. when the certificate will become valid. When the status is kSIGExpired the time
  96. is when the certificate expired. In the SIGCertInfo structure, the startDate
  97. and endDate fields hold the appropriate date information.  In the SIGSignerInfo
  98. structure, this information is provided in the certSetStatusTime field. In the
  99. SIGSignerInfo struct, the status time is actually represented by the SIGSignatureStatus
  100. field which can contain any of the types below. NOTE: The only time you will get 
  101. a kSIGInvalid status is when it pertains to a SIGSignatureStatus field and only when
  102. you get a signature that was created after the certificates expiration date, something
  103. we are not allowing on the Mac but that may not be restricted on other platforms. Also, 
  104. it will not be possible to get a kSIGPending value for SIGSignatureStatus on the Mac but
  105. possibly allowed by other platforms.
  106. */
  107. /* Values for SIGCertStatus or SIGSignatureStatus */
  108.  
  109. enum {
  110.     kSIGValid,                                                    /* possible for either a SIGCertStatus or SIGSignatureStatus */
  111.     kSIGPending,                                                /* possible for either a SIGCertStatus or SIGSignatureStatus */
  112.     kSIGExpired,                                                /* possible for either a SIGCertStatus or SIGSignatureStatus
  113.     * possible only for a SIGSignatureStatus */
  114.     kSIGInvalid
  115. };
  116.  
  117. typedef unsigned short SIGCertStatus;
  118.  
  119. typedef unsigned short SIGSignatureStatus;
  120.  
  121. /* Gestalt selector code - returns toolbox version in low-order word */
  122.  
  123. enum {
  124.     gestaltDigitalSignatureVersion = 'dsig'
  125. };
  126.  
  127. /* Number of bytes needed for a digest record when using SIGDigest */
  128. enum {
  129.     kSIGDigestSize                = 16
  130. };
  131.  
  132. typedef Byte SIGDigestData[kSIGDigestSize], *SIGDigestDataPtr;
  133.  
  134. struct SIGCertInfo {
  135.     unsigned long                    startDate;                    /* cert start validity date */
  136.     unsigned long                    endDate;                    /* cert end validity date */
  137.     SIGCertStatus                    certStatus;                    /* see comment on SIGCertStatus for definition */
  138.     unsigned long                    certAttributeCount;            /* number of name attributes in this cert */
  139.     unsigned long                    issuerAttributeCount;        /* number of name attributes in this certs issuer */
  140.     Str255                            serialNumber;                /* cert serial number */
  141. };
  142.  
  143. typedef struct SIGCertInfo SIGCertInfo;
  144.  
  145. typedef SIGCertInfo *SIGCertInfoPtr;
  146.  
  147. struct SIGSignerInfo {
  148.     unsigned long                    signingTime;                /* time of signing */
  149.     unsigned long                    certCount;                    /* number of certificates in the cert set */
  150.     unsigned long                    certSetStatusTime;            /* Worst cert status time. See comment on SIGCertStatus for definition */
  151.     SIGSignatureStatus                signatureStatus;            /* The status of the signature. See comment on SIGCertStatus for definition*/
  152. };
  153.  
  154. typedef struct SIGSignerInfo SIGSignerInfo;
  155.  
  156. typedef SIGSignerInfo *SIGSignerInfoPtr;
  157.  
  158. struct SIGNameAttributesInfo {
  159.     Boolean                            onNewLevel;
  160.     Boolean                            filler1;
  161.     SIGNameAttributeType            attributeType;
  162.     ScriptCode                        attributeScript;
  163.     Str255                            attribute;
  164. };
  165.  
  166. typedef struct SIGNameAttributesInfo SIGNameAttributesInfo;
  167.  
  168. typedef SIGNameAttributesInfo *SIGNameAttributesInfoPtr;
  169.  
  170. typedef Ptr SIGContextPtr;
  171.  
  172. typedef Ptr SIGSignaturePtr;
  173.  
  174. /*
  175. Certificates are always in order. That is, the signers cert is always 0, the
  176. issuer of the signers cert is always 1 etc… to the number of certificates-1.
  177. You can use this constant for readability in your code.
  178. */
  179.  
  180. enum {
  181.     kSIGSignerCertIndex            = 0
  182. };
  183.  
  184. /*
  185. Call back procedure supplied by developer, return false to cancel the current
  186. process.
  187. */
  188. typedef pascal Boolean (*SIGStatusProcPtr)(void);
  189.  
  190. #if GENERATINGCFM
  191. typedef UniversalProcPtr SIGStatusUPP;
  192. #else
  193. typedef SIGStatusProcPtr SIGStatusUPP;
  194. #endif
  195.  
  196. enum {
  197.     uppSIGStatusProcInfo = kPascalStackBased
  198.          | RESULT_SIZE(SIZE_CODE(sizeof(Boolean)))
  199. };
  200.  
  201. #if GENERATINGCFM
  202. #define NewSIGStatusProc(userRoutine)        \
  203.         (SIGStatusUPP) NewRoutineDescriptor((ProcPtr)(userRoutine), uppSIGStatusProcInfo, GetCurrentArchitecture())
  204. #else
  205. #define NewSIGStatusProc(userRoutine)        \
  206.         ((SIGStatusUPP) (userRoutine))
  207. #endif
  208.  
  209. #if GENERATINGCFM
  210. #define CallSIGStatusProc(userRoutine)        \
  211.         CallUniversalProc((UniversalProcPtr)(userRoutine), uppSIGStatusProcInfo)
  212. #else
  213. #define CallSIGStatusProc(userRoutine)        \
  214.         (*(userRoutine))()
  215. #endif
  216.  
  217. /*
  218. Resource id's of standard signature icon suite, all sizes and colors are available.
  219. */
  220. enum {
  221.     kSIGSignatureIconResID        = -16800,
  222.     kSIGValidSignatureIconResID    = -16799,
  223.     kSIGInvalidSignatureIconResID = -16798
  224. };
  225.  
  226. /* ——————————————————————————————— CONTEXT CALLS ——————————————————————————————— 
  227. To use the Digital Signature toolbox you will need a SIGContextPtr.  To create
  228. a SIGContextPtr you simply call SIGNewContext and it will create and initialize
  229. a context for you.  To free the memory occupied by the context and invalidate
  230. its internal data, call SIGDisposeContext. An initialized context has no notion
  231. of the type of operation it will be performing however, once you call
  232. SIGSignPrepare SIGVerifyPrepare, or SIGDigestPrepare, the contexts operation
  233. type is set and to switch  to another type of operation will require creating a
  234. new context. Be sure to pass the same context to corresponding toolbox calls
  235. (ie SIGSignPrepare, SIGProcessData, SIGSign)  in other words mixing lets say
  236. signing and verify calls with the same context is not allowed.
  237. */
  238. extern pascal OSErr SIGNewContext(SIGContextPtr *context)
  239.  FOURWORDINLINE(0x203C, 2, 1900, 0xAA5D);
  240. extern pascal OSErr SIGDisposeContext(SIGContextPtr context)
  241.  FOURWORDINLINE(0x203C, 2, 1901, 0xAA5D);
  242. /* ——————————————————————————————— SIGNING CALLS ——————————————————————————————— 
  243. Once you have created a SIGContextPtr, you create a signature by calling
  244. SIGSignPrepare once, followed by n calls to SIGProcessData, followed by one call
  245. toRcpt SIGSign. To create another signature on different data but for the same
  246. signer, don't dispose of the context and call SIGProcessData for the new data
  247. followed by a call SIGSign again. In this case the signer will not be prompted
  248. for their signer and password again as it was already provided.  Once you call
  249. SIGDisposeContext, all signer information will be cleared out of the context and
  250. the signer will be re-prompted.  The signer file FSSpecPtr should be set to nil
  251. if you want the toolbox to use the last signer by default or prompt for a signer
  252. if none exists.  The prompt parameter can be used to pass a string to be displayed
  253. in the dialog that prompts the user for their password.  If the substring "^1"
  254. (without the quotes) is in the prompt string, then the toolbox will replace it
  255. with the name of the signer from the signer selected by the user.  If an empty
  256. string is passed, the following default string will be sent to the toolbox
  257. "\pSigning as ^1.".  You can call any of the utility routines after SIGSignPrepare
  258. or SIGSign to get information about the signer or certs.
  259. */
  260. extern pascal OSErr SIGSignPrepare(SIGContextPtr context, const FSSpec *signerFile, ConstStr255Param prompt, Size *signatureSize)
  261.  FOURWORDINLINE(0x203C, 8, 1902, 0xAA5D);
  262. extern pascal OSErr SIGSign(SIGContextPtr context, SIGSignaturePtr signature, SIGStatusUPP statusProc)
  263.  FOURWORDINLINE(0x203C, 6, 1903, 0xAA5D);
  264. /* ——————————————————————————————— VERIFYING CALLS ——————————————————————————————— 
  265. Once you have created a SIGContextPtr, you verify a signature by calling
  266. SIGVerifyPrepare  once, followed by n calls to SIGProcessData, followed by one
  267. call to SIGVerify. Check the return code from SIGVerify to see if the signature
  268. verified or not (noErr is returned on  success otherwise the appropriate error
  269. code).  Upon successfull verification, you can call any of the utility routines
  270. toRcpt find out who signed the data.
  271. */
  272. extern pascal OSErr SIGVerifyPrepare(SIGContextPtr context, SIGSignaturePtr signature, Size signatureSize, SIGStatusUPP statusProc)
  273.  FOURWORDINLINE(0x203C, 8, 1904, 0xAA5D);
  274. extern pascal OSErr SIGVerify(SIGContextPtr context)
  275.  FOURWORDINLINE(0x203C, 2, 1905, 0xAA5D);
  276. /* —————————————————————————————— DIGESTING CALLS —————————————————————————————— 
  277. Once you have created a SIGContextPtr, you create a digest by calling
  278. SIGDigestPrepare once, followed by n calls to SIGProcessData, followed by one
  279. call to SIGDigest.  You can dispose of the context after SIGDigest as the
  280. SIGDigestData does not reference back into it.  SIGDigest returns the digest in
  281. digest.
  282. */
  283. extern pascal OSErr SIGDigestPrepare(SIGContextPtr context)
  284.  FOURWORDINLINE(0x203C, 2, 1906, 0xAA5D);
  285. extern pascal OSErr SIGDigest(SIGContextPtr context, SIGDigestData digest)
  286.  FOURWORDINLINE(0x203C, 4, 1907, 0xAA5D);
  287. /* —————————————————————————————— PROCESSING DATA —————————————————————————————— 
  288. To process data during a digest, sign, or verify operation call SIGProcessData
  289. as many times as necessary and with any sized blocks of data.  The data needs to
  290. be processed in the same order during corresponding sign and verify operations
  291. but does not need to be processed in the same sized chunks (i.e., the toolbox
  292. just sees it as a continuous bit stream).
  293. */
  294. extern pascal OSErr SIGProcessData(SIGContextPtr context, const void *data, Size dataSize)
  295.  FOURWORDINLINE(0x203C, 6, 1908, 0xAA5D);
  296. /* ——————————————————————————————— UTILITY CALLS ——————————————————————————————— 
  297. Given a context that has successfully performed a verification SIGShowSigner
  298. will  display a modal dialog with the entire distinguished name of the person
  299. who signed the data. the prompt (if supplied) will appear at the top of the
  300. dialog.  If no prompt is specified, the default prompt "\pVerification
  301. Successfull." will appear.
  302.  
  303. Given a context that has been populated by calling SIGSignPrepare, SIGSign or a
  304. successful SIGVerify, you can make the remaining utility calls:
  305.  
  306. SIGGetSignerInfo will return the SignerInfo record.  The certCount can be used
  307. toRcpt index into the certificate set when calling SIGGetCertInfo,
  308. SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes. The signingTime is
  309. only defined if the call is made after SIGSign  or SIGVerify. The certSetStatus
  310. will tell you the best status of the entire certificate set while
  311. certSetStatusTime will correspond to the time associated with that status (see
  312. definitions above).
  313.  
  314. SIGGetCertInfo will return the SIGCertInfo record when given a valid index into
  315. the cert set in  certIndex.  Note: The cert at index kSIGSignerCertIndex is
  316. always the signers certificate.  The  serial number, start date and end date
  317. are there should you wish to display that info.  The  certAttributeCount and
  318. issuerAttributeCount provide the number of parts in the name of that certificate
  319. or that certificates issuer respectively.  You use these numbers to index into
  320. either SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes to retrieve
  321. the name. The certStatus will tell you the status of the certificate while
  322. certStatusTime will correspond to the time associated with that status (see
  323. definitions above).
  324.  
  325. SIGGetCertNameAttributes and SIGGetCertIssuerNameAttributes return name parts
  326. of the certificate at  certIndex and attributeIndex.  The newLevel return value
  327. tells you wether the name attribute returned is at the same level in the name
  328. hierarchy as the previous attribute.  The type return value tells you  the type
  329. of attribute returned. nameAttribute is the actual string containing the name
  330. attribute.   So, if you wanted to display the entire distinguished name of the
  331. person who's signature was just validated you could do something like this;
  332.  
  333.     (…… variable declarations and verification code would preceed this sample ……)
  334.  
  335.     error = SIGGetCertInfo(verifyContext, kSIGSignerCertIndex, &certInfo);
  336.     HandleErr(error);
  337.  
  338.     for (i = 0; i <= certInfo.certAttributeCount-1; i++)
  339.         {
  340.         error = SIGGetCertNameAttributes(
  341.             verifyContext, kSIGSignerCertIndex, i, &newLevel, &type, theAttribute);
  342.         HandleErr(error);
  343.         DisplayNamePart(theAttribute, type, newLevel);
  344.         }
  345. */
  346. extern pascal OSErr SIGShowSigner(SIGContextPtr context, ConstStr255Param prompt)
  347.  FOURWORDINLINE(0x203C, 4, 1909, 0xAA5D);
  348. extern pascal OSErr SIGGetSignerInfo(SIGContextPtr context, SIGSignerInfo *signerInfo)
  349.  FOURWORDINLINE(0x203C, 4, 1910, 0xAA5D);
  350. extern pascal OSErr SIGGetCertInfo(SIGContextPtr context, unsigned long certIndex, SIGCertInfo *certInfo)
  351.  FOURWORDINLINE(0x203C, 6, 1911, 0xAA5D);
  352. extern pascal OSErr SIGGetCertNameAttributes(SIGContextPtr context, unsigned long certIndex, unsigned long attributeIndex, SIGNameAttributesInfo *attributeInfo)
  353.  FOURWORDINLINE(0x203C, 8, 1912, 0xAA5D);
  354. extern pascal OSErr SIGGetCertIssuerNameAttributes(SIGContextPtr context, unsigned long certIndex, unsigned long attributeIndex, SIGNameAttributesInfo *attributeInfo)
  355.  FOURWORDINLINE(0x203C, 8, 1913, 0xAA5D);
  356. /* ——————————————————————————— FILE SIGN & VERIFY CALLS —————————————————————————— 
  357. These calls allow you to detect the presence of a standard signtaure in a file as 
  358. well as sign and verify files in a standard way.  An example of this is the Finder, 
  359. which uses these calls to allow the user to "drop sign" a file.
  360.  
  361. To detect if a file is signed in the standard way, pass the FSSpec of the file to SIGFileIsSigned.
  362. A result of noErr means the file is in fact signed, otherwise, a kSIGNoSignature error will
  363. be returned.
  364.  
  365. Once you have created a SIGContextPtr, you can make calls to either sign or verify a file in
  366. a standard way: 
  367.  
  368. To sign a file, call SIGSignPrepare followed by 'n' number of calls to SIGSignFile,
  369. passing it the file spec for each file you wish to sign in turn.  You supply the context, the signature 
  370. size that was returned from SIGSignPrepare and an optional call back proc.  The call will take care of all
  371. the processing of data and affixing the signature to the file. If a signature already exists in the file, 
  372. it is replaced with the newly created signature.
  373.  
  374. To verify a file that was signed using SIGSignFile, call SIGVerifyFile passing it a new context and 
  375. the file spec.  Once this call has completed, if the verification is successfull, you can pass the context 
  376. to SIGShowSigner to display the name of the person who signed the file.
  377. */
  378. extern pascal OSErr SIGFileIsSigned(const FSSpec *fileSpec)
  379.  FOURWORDINLINE(0x203C, 2, 2500, 0xAA5D);
  380. extern pascal OSErr SIGSignFile(SIGContextPtr context, Size signatureSize, const FSSpec *fileSpec, SIGStatusUPP statusProc)
  381.  FOURWORDINLINE(0x203C, 8, 2501, 0xAA5D);
  382. extern pascal OSErr SIGVerifyFile(SIGContextPtr context, const FSSpec *fileSpec, SIGStatusUPP statusProc)
  383.  FOURWORDINLINE(0x203C, 6, 2502, 0xAA5D);
  384.  
  385. #if PRAGMA_IMPORT_SUPPORTED
  386. #pragma import off
  387. #endif
  388.  
  389. #if PRAGMA_ALIGN_SUPPORTED
  390. #pragma options align=reset
  391. #endif
  392.  
  393. #ifdef __cplusplus
  394. }
  395. #endif
  396.  
  397. #endif /* __DIGITALSIGNATURE__ */
  398.